home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte25 / ex22.c < prev    next >
C/C++ Source or Header  |  1995-04-30  |  2KB  |  73 lines

  1. #include <genstub.c>
  2.  
  3. DWORD TLSIndex = 0;
  4. BOOL  fAbort;
  5.  
  6. // Show progress without invoking waiting thread.
  7. VOID ShowRunningThread( HWND hWnd )
  8. {
  9.    HDC hDC = GetDC( hWnd );
  10.    TextOut(hDC, 0, 0, TlsGetValue( TLSIndex ), lstrlen( TlsGetValue( TLSIndex ) ) );
  11.    ReleaseDC( hWnd, hDC );
  12. }
  13.  
  14. // Thread procedure. This function just prints a message once every half-second.
  15. DWORD WINAPI ThreadProc( HWND hWnd )
  16. {
  17.    LPVOID lpVoid = HeapAlloc( GetProcessHeap(), 0, 128 );
  18.    wsprintf( lpVoid, "Instance %8lx is now running.   ", GetCurrentThreadId() );
  19.    TlsSetValue( TLSIndex, lpVoid );
  20.    while ( !fAbort )
  21.    {
  22.       Sleep( 500 );
  23.       ShowRunningThread( hWnd );
  24.       Sleep( 0 );
  25.    }
  26.    HeapFree( GetProcessHeap(), 0, lpVoid );
  27.    ExitThread( TRUE );
  28. }
  29.  
  30. // Windows message procedure.
  31. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  32. {
  33.    switch (uMsg)
  34.    {
  35.          case WM_CREATE:
  36.                  TLSIndex = TlsAlloc();
  37.                  break;
  38.          case WM_DESTROY:
  39.                  TlsFree( TLSIndex );
  40.                  PostQuitMessage( 0 );
  41.                  break;
  42.          case WM_COMMAND:
  43.                  switch ( LOWORD( wParam ) )
  44.                  {
  45.                     case IDM_TEST:
  46.                        {
  47.                           int i;
  48.                           HANDLE hThreads[4];  // Array of threads to wait for.
  49.                           DWORD dwChildID;     // Child ID - discarded.
  50.                           fAbort = FALSE;
  51.                           for (i=0; i<4; i++)
  52.                              hThreads[i] = CreateThread( NULL, 0, ThreadProc,
  53.                                                          hWnd, 0, &dwChildID );
  54.                           // Wait half a minute.
  55.                           WaitForMultipleObjects( 4, &hThreads, TRUE, 28000 );
  56.                           fAbort = TRUE;
  57.                           WaitForMultipleObjects( 4, &hThreads, TRUE, 2000 );
  58.                           // Clean screen.
  59.                           InvalidateRect( hWnd, NULL, TRUE );
  60.                           UpdateWindow( hWnd );
  61.                        }
  62.                        break;
  63.                     case IDM_EXIT:
  64.                           DestroyWindow( hWnd );
  65.                           break;
  66.                  }
  67.                  break;
  68.  
  69.          default:
  70.                  return (DefWindowProc(hWnd, uMsg, wParam, lParam));
  71.    }
  72.    return (NULL);
  73. }